﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using xbmc_json_async;
using xbmc_json_async.System;
using Newtonsoft.Json.Linq;

namespace xbmc_json_async.Utilities
{
    public class Injector
    {
        public void Inject(object fillSource)
        {
            Type objectType = this.GetType();
        }
        public object FillObject(object sourceObject, object fillSource)
        {
            Type objectType = sourceObject.GetType();
            PropertyInfo[] propertyInfo = objectType.GetProperties();

            foreach (PropertyInfo info in propertyInfo)
            {
                Type propertyType = info.PropertyType;
                string memberName = info.Name;

                if (info.CanWrite)
                {
                    if (info.PropertyType.FullName == "System.String")
                    {
                        try
                        {
                            string setVal = XHelpers.Jval((JObject)fillSource, memberName);
                            info.SetValue(sourceObject, setVal, null);
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                    if (info.PropertyType.FullName == "System.Int32")
                    {
                        try
                        {
                            Int32 setVal = XHelpers.JvalInt((JObject)fillSource, memberName);
                            info.SetValue(sourceObject, setVal, null);
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                    if (info.PropertyType.FullName == "System.Double")
                    {
                        try
                        {
                            Double setVal = XHelpers.JValDouble((JObject)fillSource, memberName);
                            info.SetValue(sourceObject, setVal, null);
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                    
                }
            }
            return sourceObject;

        }
    }
}
